package com.pro.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pro.dao.impl.MessageDaoImpl;
import com.pro.dao.impl.NewsDaoImpl;
import com.pro.dao.impl.NewsTypeDaoImpl;
import com.pro.entity.MessageEntity;
import com.pro.entity.NewsEntity;
import com.pro.util.BaseServlet;
import com.pro.util.DateUtil;
import com.pro.util.Result;

/**
 * Servlet implementation class NewsAdminServlet
 */
public class MessageAdminServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
	NewsDaoImpl nDaoImpl = new NewsDaoImpl();
	MessageDaoImpl mDaoImpl = new MessageDaoImpl();
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void messageList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 新闻资讯
        ArrayList<MessageEntity> aList = mDaoImpl.messageAdminList();
		
		PrintWriter out=response.getWriter();
		out.write(com.pro.util.ResultToLayui.toClient(0, "", aList.size(), aList));
		out.flush();
		out.close();
	}
	
	public void messageUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		int id= Integer.parseInt(request.getParameter("messageId"));
		  String messageName = request.getParameter("messageName");
		  String messagePhone = request.getParameter("messagePhone");
		  String messageEmail = request.getParameter("messageEmail");
		  String showHome = request.getParameter("showHome");
		String messageContent = request.getParameter("messageContent");
		  String messageNote = request.getParameter("messageNote");
		  Date date = new Date();
		  String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		  int result = mDaoImpl.messageUpdate(new MessageEntity(id, createTime, messageName, messagePhone, messageEmail, messageContent, showHome, messageNote, createTime));
		  
		  PrintWriter out = response.getWriter(); 
		  out.print(Result.toClient("修改", result>0?true:false));
		  out.flush(); 
		  out.close(); 
	}
	
	public void messageDelete(HttpServletRequest request,HttpServletResponse response) throws Exception {
		int id= Integer.parseInt(request.getParameter("messageId"));
		int result=mDaoImpl.messageDelete(id);
		// 我们把数据输出到页面上
		PrintWriter printWriter = response.getWriter();
		printWriter.print(Result.toClient("删除",result>0?true:false));
		printWriter.flush();
		printWriter.close();
      }

}
